package com.opencloud.iot.provider.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.opencloud.common.model.PageParams;
import com.opencloud.common.model.ResultBody;
import com.opencloud.iot.client.model.entity.IotConfigs;
import com.opencloud.iot.client.model.entity.IotMoveObserver;
import com.opencloud.iot.provider.service.IotConfigsService;
import com.opencloud.iot.provider.service.IotMoveObserverService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.Map;

import static java.util.Objects.isNull;

/**
 * 物联设备 过程数据管理
 * @author yeyong
 */
@RestController
@Api(value = "物联网设备", tags = "物联网设备 过程数据管理")
public class IotMoveObserverController {

    @Autowired
    private IotMoveObserverService iotMoveObserverService;

    @Autowired
    private IotConfigsService iotConfigsService;

    /**
     * 获取运动过程数据
     * @param map
     * @return
     */
    @ApiOperation(value = "获取运动过程数据", notes = "获取运动过程数据")
    @GetMapping("/MoveObserver/infos")
    public ResultBody<IPage<IotMoveObserver>> getMoveObserverListPage(@RequestParam(required = false) Map map){
        return ResultBody.ok().data(iotMoveObserverService.findListPage(new PageParams(map)));
    }

    /**
     * 生成运动过程数据
     * @param startTime
     * @param endTime
     * @return
     */
    @ApiOperation(value = "生成运动过程数据", notes = "生成运动过程数据")
    @GetMapping("/moveObserver/createData")
    public ResultBody<Integer> createObserverData(
            @RequestParam(value = "startTime",required = false) Long startTime,
            @RequestParam(value = "endTime",required = false) Long endTime)
    {
        IotConfigs configs = iotConfigsService.findConfigByKey("MOVE");
        int timeslot = 600;
        if(configs!=null)
        {
            timeslot = configs.getConfigTimevalue()*60;
        }
        if(ObjectUtils.isEmpty(startTime))startTime=new Date().getTime()/1000-timeslot;
        if(ObjectUtils.isEmpty(endTime))endTime=new Date().getTime()/1000;
        System.out.println(startTime);
        System.out.println(endTime);
        return ResultBody.ok().data(iotMoveObserverService.createMoveObserverList(startTime,endTime));
    }
}
